
export const formatSendTime = (sendTime) => {
	const length = sendTime.toString().length;

	if (length === 10) {
		sendTime = sendTime * 1000;
	}
	const now = new Date()
	
	const sendTimestamp = parseInt(sendTime);
	
	const sendDate = new Date(sendTimestamp)
	// 计算时间差（以毫秒为单位）
	const timeDiff = now - sendDate
	// console.log(timeDiff, '时间差')
	const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate())
	const startOfTargetDate = new Date(sendDate.getFullYear(), sendDate.getMonth(), sendDate.getDate())
	// 一天内的毫秒数
	const oneDay = 24 * 60 * 60 * 1000
	// 如果发送时间在当前时间之前
	if (timeDiff < 0) {
		// return 'Invalid time' // 或者其他错误处理
		return formatTime(sendDate)
	}
	// 发生在同一天
	if (startOfToday.getTime() === startOfTargetDate.getTime()) {
		return formatTime(sendDate)
	}
	// 如果发送时间在一天内
	if (timeDiff < oneDay) {
		return getApp().$t("common.time.yesterday")+' ' + formatTime(sendDate)
	}
	// 如果发送时间在二天至七天内
	if (timeDiff < 7 * oneDay) {
		const weekday = getWeekday(sendDate)
		return weekday + ' ' + formatTime(sendDate)
	}
	let locale = uni.getLocale()
	// 如果发送时间超过七天
	return (
		sendDate.getFullYear() + "-" + JSON.stringify(sendDate.getMonth() + 1).padStart(2, 0) + "-" + JSON.stringify(sendDate.getDate()).padStart(2, 0) + ' ' + formatTime(sendDate)
	)
}

function formatTime(date) {
	// 格式化时间为“时:分”
	const hours = date.getHours().toString().padStart(2, '0')
	const minutes = date.getMinutes().toString().padStart(2, '0')
	return hours + ':' + minutes
}

function getWeekday(date) {
	// 获取星期几的中文表示
	const weekdays = [getApp().$t("common.time.sunday"), getApp().$t("common.time.monday"), getApp().$t("common.time.tuesday"), getApp().$t("common.time.wednesday"), getApp().$t("common.time.thursday"), getApp().$t("common.time.friday"), getApp().$t("common.time.saturday")]
	return weekdays[date.getDay()]
}


export const getDateParts = (timestamp) => {
	const length = timestamp.toString().length;
	
	if (length === 10) {
		timestamp = timestamp * 1000;
	}
    let date = new Date(timestamp);
    let currentYear = (new Date()).getFullYear(); // 获取当前年份

    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    let day = date.getDate();

    // 如果时间戳对应的年份与当前年份相同，则不返回年份信息
    if (year === currentYear) {
        return {
            month: month,
            day: day
        };
    } else {
        return {
            year: year,
            month: month,
            day: day
        };
    }
}

// const dateUtils = {
//     formatSendTime,
//     getDateParts
// };

// export default formatSendTime